package com.yitongmao.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.adapter.MyFoucusListAdapter;
import com.yitongmao.bean.Guanzhu;
import com.yitongmao.bean.LoginHuancunBean;
import com.yitongmao.bean.MyFoucusListBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.cache.ACache;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.SignUtil;
import com.yitongmao.util.ToastUtils;
import com.yitongmao.yitmfruit.R;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyAttentionActivity extends AppCompatActivity implements MyFoucusListAdapter.CheckInter {
    @Bind(R.id.mBianji)
    TextView mBianji;
    @Bind(R.id.mMyAttentionCompile)
    AutoLinearLayout mMyAttentionCompile;//编辑还是删除
    @Bind(R.id.mCancel)
    Button mCancel;
    private boolean flag;
    private List<MyFoucusListBean.GoodsBean> goods;
    private LoginHuancunBean loginHuancunBean;
    private MyFoucusListAdapter myFoucusListAdapter;
    private ListView mMyAttenTionList;
    private AutoLinearLayout mRedaihuitui;
    private int userId;
    private int loginId1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerSion();
        setContentView(R.layout.activity_my_attention);
        ButterKnife.bind(this);
        Gson gson = new Gson();
        String loginId = ACache.get(MyAttentionActivity.this).getAsString("loginId");
        if (loginId != null) {
            loginHuancunBean = gson.fromJson(loginId, LoginHuancunBean.class);
            userId = loginHuancunBean.userInfo.id;
            loginId1 = loginHuancunBean.userInfo.loginId;
            myFocusList(userId);

        }

        initView();
        setOnClick();
    }

    private void initView() {
        mMyAttenTionList = (ListView) findViewById(R.id.mMyAttenTionList);
        mRedaihuitui = (AutoLinearLayout) findViewById(R.id.mRedaihuitui);

    }

    private void setOnClick() {
        mRedaihuitui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyAttentionActivity.this.finish();
            }
        });
        mMyAttentionCompile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flag = !flag;
                if (flag) {//在删除界面
                    mBianji.setText("完成");
                    mCancel.setVisibility(View.VISIBLE);
                    //myFoucusListAdapter.notifyDataSetChanged();
                    myFoucusListAdapter.isShow(false);
                } else {//查看列表页
                    mBianji.setText("编辑");
                    mCancel.setVisibility(View.GONE);
                    myFoucusListAdapter.isShow(true);
                }
            }
        });
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s="";
                StringBuffer stringBuffer=new StringBuffer();
                for (int i=goods.size()- 1;i >= 0; i--){
                    if (goods.get(i).isDelCheck){
                        stringBuffer.append(goods.get(i).followId).append(",");
                        goods.remove(i);
                    }
                }
                s=stringBuffer.toString().substring(0, (stringBuffer.toString().length() - 1));
                if (!s.equals("")){
                    shoppingListNetWork(s,String.valueOf(userId));
                }
                myFoucusListAdapter.notifyDataSetChanged();
            }
        });
    }

    public void myFocusList(int userId) {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("userId", String.valueOf(userId));
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("signLogin",SignUtil.signLogin(nonce,timestamp));
        params.put("loginId",String.valueOf(loginId1));
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.myFocusUrlList, params, new CallBackListener() {
            @Override
            public void onStart() {
            }
            @Override
            public void onSuccess(Object result) {
                String string = (String) result;
                // Log.e("Gson", "" + string);
                Gson gson = new Gson();
                MyFoucusListBean myFoucusListBean = gson.fromJson(string, MyFoucusListBean.class);
                if (myFoucusListBean.result.state.equals("1")) {
                   // MyFoucusListAdapter myFoucusListAdapter=new MyFoucusListAdapter(MyAttentionActivity.this,goods);
                    goods = myFoucusListBean.goods;
                    myFoucusListAdapter = new MyFoucusListAdapter(MyAttentionActivity.this, goods);
                    mMyAttenTionList.setAdapter(myFoucusListAdapter);
                    myFoucusListAdapter.setCheckInterface(MyAttentionActivity.this);
                }
            }

            @Override
            public void onFail(Object result) {
                System.out.print("失败");
            }
        });
    }

    //沉浸式状态栏
    public void immerSion() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
    }
    //监听选中
    @Override
    public void checkGroup(int position, boolean isChecked) {
        if (flag) {
            goods.get(position).setDelCheck(isChecked);
        }
    }
    public void shoppingListNetWork(String ids,String userId) {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("ids", ids);
        params.put("userId", userId);
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("signLogin",SignUtil.signLogin(nonce,timestamp));
        params.put("loginId",String.valueOf(loginId1));
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.deleteAmendUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }
            @Override
            public void onSuccess(Object result) {
                String string = (String) result;
                Gson gson = new Gson();
                Guanzhu guanzhu = gson.fromJson(string, Guanzhu.class);
                ToastUtils.showShort(MyAttentionActivity.this,""+guanzhu.result.message);
                Log.e("position",""+string);
                myFoucusListAdapter.notifyDataSetChanged();
            }
            @Override
            public void onFail(Object result) {
                System.out.print("失败");
            }
        });
    }
}
